<?
function deleteFile($fID) {	global $dbname, $db, $db_selected;
	// Get file information	$sql = "SELECT * from casa_uploads WHERE FID = '$fID'";	$result = mysql_db_query($dbname,$sql);			$row = mysql_fetch_assoc($result);
	$fileName = ROOT_DIR.$row[FileName];	// Delete file		@unlink($fileName);
	// Delete file information	$sql = "DELETE from casa_uploads WHERE FID = '$fID'";	$result = mysql_db_query($dbname,$sql);	
	return $fID;}
function saveReg($phpString,$eID,$doWhat,$regUserID=null) {	global $returnString,$dbname, $db, $db_selected;	// Parse PHP String	$valueArray = explode('|',$phpString);
	$programID = $valueArray[0];	$nameFirst = fieldToDB($valueArray[1]);	$nameLast  = fieldToDB($valueArray[2]);	$phone     = fieldToDB($valueArray[3]);	$email     = fieldToDB($valueArray[4]);	$position  = fieldToDB($valueArray[5]);	$emailHidden = $valueArray[6];
	if ($phone == 'Phone Number') {		$phone = '';	}	if ($email == 'Email Address') {		$email = '';	}	if ($email == '[hidden]') {		$email = $emailHidden;	}
	if ($doWhat == 'add') {		// Is this person already in the user table?		$sql = "SELECT UID from casa_users WHERE NameFirst = '$nameFirst' AND NameLast = '$nameLast' AND Email = '$email'";		$result = mysql_db_query($dbname,$sql);		if (mysql_num_rows($result)<1) {			$sql = "INSERT into casa_users (					ProgramID,					NameFirst,					NameLast,					Email,					Phone1,					PositionID,
					EventReg					) values (					'$programID',					'$nameFirst',					'$nameLast',					'$email',					'$phone',					'$position',
					'1'					)";			$result = mysql_db_query($dbname,$sql);
			// Get UID for this person			$uID = mysql_insert_id();					} else {					$uIDRow = mysql_fetch_assoc($result);			$uID = $uIDRow[UID];		}	
		// Enter record into casa_event_registration		$sql = "INSERT into casa_event_registration (					EID,					UID					) values (					'$eID',					'$uID'					)";		$result = mysql_db_query($dbname,$sql);
		$string  = '<table style="width: 100%;">				<tr>					<td style="width: 50px;"><img src="images/button_delete.gif" alt="Delete" onClick="deleteReg('.$uID.');" style="cursor: pointer;" /></td>';		$string .= '<td><input type="text" id="nameFirst'.$uID.'" value="'.$nameFirst.'" style="width: 120px;" /></td>';		$string .= '<td><input type="text" id="nameLast'.$uID.'" value="'.$nameLast.'" style="width: 120px;" /></td>';		$string .= '<td><input type="text" id="email'.$uID.'" value="'.$email.'" style="width: 150px;" /></td>';		$string .= '<td><input type="text" id="phone'.$uID.'" value="'.$phone.'" style="width: 120px;" /></td>';		$string .= '<td><select id="position'.$uID.'" size="1" style="width: 100px;">						<option>Position...</option>						<option></option>';
		// Render positions		$positions = getCategories('select','P',$position);		$string .= $positions;		$string .= '</select>					</td>';		$string .= '<td style="width: 80px;"><img src="images/buttonEdit.gif" alt="Edit" onClick="saveReg(\'edit\','.$uID.');" style="cursor: pointer;" /></td>				</tr>				</table>';		$returnString = $string.'|'.$uID;	} else {		// We're editing a record now				$uID = $regUserID;
		$sql = "UPDATE casa_users SET				ProgramID  = '$programID',				NameFirst  = '$nameFirst',				NameLast   = '$nameLast',				Email      = '$email',				Phone1     = '$phone',				PositionID = '$position'				WHERE					UID = '$uID'";		$result = mysql_db_query($dbname,$sql);
		$returnString = 'null|'.$uID;		}
	
	// Send email to user and admins
	sendMailRegistrationInfo($email,$uID,$eID);
	return $returnString;}
function deleteReg($uID,$eID) {
	global $dbname, $db, $db_selected;
	$sql = "DELETE from casa_event_registration WHERE EID = '$eID' AND UID = '$uID'";	$result = mysql_db_query($dbname,$sql);}
function getRegistered($progID,$eID) {
	global $dbname, $db, $db_selected;
	$registered = '';	$sql = "SELECT U.UID,U.NameFirst,U.NameLast,U.Email,U.Phone1,U.PositionID			FROM casa_event_registration ER 			LEFT JOIN casa_users U ON ER.UID = U.UID			WHERE 				ER.EID = '$eID' AND				U.ProgramID = '$progID'			ORDER BY NameLast";	$result = mysql_db_query($dbname,$sql);	$rows = mysql_num_rows($result);
	if ($rows>0) {		while ($value = mysql_fetch_assoc($result)) {			$uID = $value[UID];			$nameFirst = $value[NameFirst];			$nameLast  = $value[NameLast];			$email     = $value[Email];			$phone     = $value[Phone];			$position  = $value[PositionID];						$registered .= '<div style="padding: 3px;" id="person'.$uID.'"><table style="width: 100%;">					<tr>						<td style="width: 50px;"><img src="images/button_delete.gif" alt="Delete" onClick="deleteReg('.$uID.');" style="cursor: pointer;" /></td>';			$registered .= '<td><input type="text" id="nameFirst'.$uID.'" value="'.$nameFirst.'" style="width: 120px;" /></td>';			$registered .= '<td><input type="text" id="nameLast'.$uID.'" value="'.$nameLast.'" style="width: 120px;" /></td>';			$registered .= '<td><input type="text" id="email'.$uID.'" value="[hidden]" style="width: 150px;" /><input type="hidden" id="emailHidden'.$uID.'" value="'.$email.'" /></td>';			$registered .= '<td><input type="text" id="phone'.$uID.'" value="'.$phone.'" style="width: 120px;" /></td>';			$registered .= '<td><select id="position'.$uID.'" size="1" style="width: 100px;">							<option>Position...</option>							<option></option>';						// Render positions			$positions = getCategories('select','P',$position);			$registered .= $positions;			$registered .= '</select>						</td>';			$registered .= '<td style="width: 80px;"><img src="images/buttonEdit.gif" alt="Edit" onClick="saveReg(\'edit\','.$uID.');" style="cursor: pointer;" /></td>					</tr>					</table></div>';		}	
	} else {		$registered .= ''; 	}	return $registered;}
function getProgramDetails($programID) {
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT * from casa_programs WHERE PID = '$programID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$programName = stripslashes($row[ProgramName]);
	$pCounty     = stripslashes($row[County]);
	$pAddress    = stripslashes($row[Address]);
	$pCity       = $row[City];
	$pZip        = $row[Zip];
	
	$returnString = '<h2>'.$programName.'</h2>';	
	if (!empty($pCounty)) { 
		$returnString .= 'County: '.$pCounty.'<br />';
	}
	if (!empty($pAddress)) {
		$returnString .= $pAddress.'<br />';
	}
	if (!empty($pCity)) {
		$returnString .= $pCity.' '.$pZip;
	}
	
	return $returnString;

}

function getUserDetails($uID) {
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT CU.NameFirst,CU.NameLast,CU.Email,CU.Phone1,CU.Phone2,CU.OfficeNo,C.CatName AS Position 
		FROM casa_users CU 
		LEFT JOIN casa_categories C ON CU.PositionID = C.CID
		WHERE CU.UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$position  = $row[Position];
	$programID = $row[ProgramID];
	$nameFirst = $row[NameFirst];
	$nameLast  = $row[NameLast];
	$email     = $row[Email];
	$phone1    = $row[Phone1];
	$phone2    = $row[Phone2];
	$officeNo  = $row[OfficeNo];
	$positionID= $row[PositionID];
	
	$userArray = array(
					$uID,
					$nameFirst,
					$nameLast,
					$email,
					$phone1,
					$phone2,
					$officeNo,
					$positionID,
					$position,
					$programID
					);
	// TODO: Need to determine priveledges $editrRogram, $editContacts, $addContact
	// We can get this from contacts.php
	return $userArray;		

}

function getEventDetails($eID) {
	global $dbname, $db, $db_selected;
	
	// Get event information
	$sql = "SELECT 
				E.ECat,
				E.Userid,
				E.EventTitle,
				E.EventLocation,
				E.EventDesc,
				E.EventURL,
				E.EventRegURL,
				E.DateStart,
				E.DateEnd,
				E.EventTimes,
				E.Registration,
				E.Active,
				C.CatName 
			FROM casa_events E
			LEFT JOIN casa_categories C ON E.ECat = C.CID
			WHERE E.EID = '$eID'";				
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$eventArray = array(
						$eID,
						$row[ECat],
						$row[CatName],
						$row[Userid],
						stripslashes($row[EventTitle]),
						stripslashes($row[EventLocation]),
						stripslashes($row[EventDesc]),
						$row[EventURL],
						$row[EventRegURL],
						$row[DateStart],
						$row[DateEnd],
						$row[EventTimes],
						$row[Registration],
						$row[Active]
						);
	
	return $eventArray;
}

function saveProgramAddress($phpString) {
	global $dbname, $db, $db_selected;
	
	$phpArray = explode('|',$phpString);
	$pID      = $phpArray[0];
	$pName    = fieldToDB($phpArray[1]);
	$pCounty  = fieldToDB($phpArray[2]);
	$pAddress = fieldToDB($phpArray[3]);
	$pCity    = fieldToDB($phpArray[4]);
	$pZip     = fieldToDB($phpArray[5]);
	
	$sql = "UPDATE casa_programs SET
				ProgramName = '$pName',
				County      = '$pCounty',
				Address     = '$pAddress',
				City        = '$pCity',
				Zip         = '$pZip'
			WHERE 
			PID = '$pID'";
	$result = mysql_db_query($dbname,$sql);
	$returnString = getProgramDetails($pID);
	return $returnString;
}

function getProgramForEdit($programID) {
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT * from casa_programs WHERE PID = '$programID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$pName    = stripslashes($row[ProgramName]);
	$pCounty  = stripslashes($row[County]);
	$pAddress = DBToField($row[Address]);
	$pCity    = stripslashes($row[City]);
	$pZip     = stripslashes($row[Zip]); 
	
	$returnString  = '<table><tr><td>Program Name</td><td><input type="text" id="pName" value="'.$pName.'" style="width: 300px;" /></td></tr>';
	$returnString .= '<tr><td>County</td><td><input type="text" id="pCounty" value="'.$pCounty.'" style="width: 300px;" /></td></tr>';
	$returnString .= '<tr><td style="vertical-align: top;">Address</td><td><textarea id="pAddress" style="width: 300px; height: 40px;" />'.$pAddress.'</textarea></td></tr>';
	$returnString .= '<tr><td>City</td><td><input type="text" id="pCity" value="'.$pCity.'" style="width: 300px;" /></td></tr>';
	$returnString .= '<tr><td>Zip Code</td><td><input type="text" id="pZip" value="'.$pZip.'" style="width: 115px;" /></td></tr>';
	$returnString .= '<tr><td></td><td><input type="hidden" id="pID" value="'.$programID.'" /> <input type="button" value="Save" onClick="saveProgramAddress();" /> <input type="button" value="Cancel" onClick="cancelSaveProgramAddress();" /></td></tr></table>';
	
	return $returnString;
}

function getContactForEdit($uID) {
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT * FROM casa_users WHERE UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$programID = $row[ProgramID];
	$nameFirst = $row[NameFirst];
	$nameLast  = $row[NameLast];
	$email     = $row[Email];
	$phone1    = $row[Phone1];
	$phone2    = $row[Phone2];
	$officeNo  = $row[OfficeNo];
	$positionID= $row[PositionID];
	
	$returnString  = '<div style="padding: 2px; background: #EBF4FD;">';
	$returnString .= '<table style="width: 100%;">';
	
	$returnString .= '<tr><td>Name</td>';
	$returnString .= '<td><input type="text" id="nameFirst'.$uID.'" style="width: 120px;" value="'.$nameFirst.'" />&nbsp;';
	$returnString .= '<input type="text" id="nameLast'.$uID.'" style="width: 120px;" value="'.$nameLast.'" /></td></tr>';
	
	$returnString .= '<tr><td>Program</td>';
	$returnString .= '<td><select id="programID'.$uID.'" size="1" style="width: 246px;">';
	$returnString .= getPrograms('select',$programID);
	$returnString .= '</select></td></tr>';	
	
	$returnString .= '<tr><td>Position</td>';
	$returnString .= '<td><select id="positionID'.$uID.'" style="width: 246px;" size="1">';
	$returnString .=  getCategories('select','P',$positionID);
	$returnString .= '</select></td></tr>';
	
	$returnString .= '<tr><td>Email</td>';
	$returnString .= '<td><input type="text" style="width: 246px;" value="'.$email.'" id="email'.$uID.'" /></td></tr>';
	
	$returnString .= '<tr><td>Phone, Office</td>';
	$returnString .= '<td><input type="text" style="width: 246px;" value="'.$phone1.'" id="phone1'.$uID.'" /></td></tr>';
	
	$returnString .= '<tr><td>Phone, Mobile</td>';
	$returnString .= '<td><input type="text" style="width: 246px;" value="'.$phone2.'" id="phone2'.$uID.'" /></td></tr>';
	
	$returnString .= '<tr><td>Office No.</td>';
	$returnString .= '<td><input type="text" style="width: 100px;" value="'.$officeNo.'" id="officeNo'.$uID.'" /></td></tr>';
	
	$returnString .= '<tr><td></td><td><input type="button" value="Save" onClick="saveProgramContact('.$uID.');" /> <input type="button" value="Cancel" onClick="cancelEditProgramContact('.$uID.');" /></td></tr>';
	
	$returnString .= '</table></div>';
	
	return $returnString.'|'.$uID;
}

function saveProgramContact($phpString) {
	global $dbname, $db, $db_selected;
	
	$phpArray = explode('|',$phpString);
	$uID = $phpArray[0];
	$nameFirst = fieldToDB($phpArray[1]);
	$nameLast  = fieldToDB($phpArray[2]);
	$programID = $phpArray[3];
	$email     = fieldToDB($phpArray[4]);
	$phone1    = fieldToDB($phpArray[5]);
	$phone2    = fieldToDB($phpArray[6]);
	$officeNo  = fieldToDB($phpArray[7]);
	$positionID= $phpArray[8];
	
	$sql = "UPDATE casa_users SET 
				ProgramID = '$programID',
				NameFirst = '$nameFirst',
				NameLast  = '$nameLast',
				Email     = '$email',
				Phone1    = '$phone1',
				Phone2    = '$phone2',
				OfficeNo  = '$officeNo',
				PositionID= '$positionID',
				DateUpdate= '".date('Y-m-d')."'
			WHERE
			UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	
	return renderUserDetails(getUserDetails($uID)).'|'.$uID;
}

function deleteProgramContact($uID) {
	global $dbname, $db, $db_selected;
	
	$sql = "DELETE from casa_users WHERE UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	
	return $uID;
}
?>